<?php

/**
 * 建议
 * ============================================================================
 * * 版权所有 2012 铭迈（上海）实业有限公司，并保留所有权利。
 * 网站地址: http://www.mingmailtd.com/；
 * ----------------------------------------------------------------------------
 * 仅用于公司内部人员使用。
 * ============================================================================
 * $Author: zhangmingming $
 * $Id: lib_smart.php 2012/6/27 15:34:03 $
 */

define('IN_MMOA', true);

require(dirname(__FILE__) . '/includes/init.php');

$username = trim($_SESSION['username']);

// 默认显示我的建议
if ($_REQUEST['act'] == '')
{
	$sql = "SELECT id FROM " . $mmoa->table('employees') . 
			" WHERE e_name = '" . $_SESSION['username'] . "'";
	$id = $db->getOne($sql);
	
	$suggest = suggest_show($id);
	$smarty->assign('suggest', $suggest);
	
	//$departments = department_show(); //print_r($departments);
	//$smarty->assign('departments', $departments);
	
	$smarty->assign('mm_show', 'MySuggest');
	
	clear_caches();
	$smarty->display('suggest.tpl');
}
// 显示提交列表
elseif ($_REQUEST['act'] == 'add')
{
	$smarty->assign('mm_show', 'AddSuggest');
	
	clear_caches();
	$smarty->display('suggest.tpl');
}
// 处理提交建议
elseif ($_REQUEST['act'] == 'update')
{
	$c = $_POST['content'];  echo $c;
	
	$file = $_FILES['sugfile']; //print_r($file); exit;
	$upload_dir = 'upload/sugg/';
	$upload_name = $upload_dir.$file['name'];
	
	if (!empty($file['name'])){
		if($file['error'] > 0){
			echo '<meta charset="utf-8">';
			echo '文件上传错误：'. $file['error'];
			exit();
		} else {
			//echo '文件名：' . $_FILES['devfile']['name']  . '<br />';
			//echo '类型：' .  $file['type'] . '<br />';
			//echo '大小：' .  $file['size'] . '<br />';
			if (file_exists($upload_name)){
				echo '<meta charset="utf-8">';
				echo $file['name'] . '文件已经存在。';
				exit();
			} else {
				move_uploaded_file($file['tmp_name'], $upload_name);
				echo '文件已经被存储到：' . $upload_name;
			}
	
			$suggfile = $upload_name;
		}
	} else {
		$suggfile = '';
	}
	
	if(!empty($c)){
	  $sql = "SELECT id FROM " . $mmoa->table('employees') . " WHERE e_name = '$username'";
	  $eid = $db->getOne($sql);
	  
	  $date = date("Y-m-d"); //'2012-06-29';
	  
	  $sqlu = "INSERT INTO ". $mmoa->table('suggest') ."(`s_eid`, `s_content`, `s_date`, `s_file`) VALUES ('$eid','$c','$date','$suggfile')";
	  
	  if($db->query($sqlu)){
	  	// 提交成功
	  	//echo 'ok';
	  	header("Location: suggest.php");
	  } else {
	  	//echo 'error';
	  	header("Location: suggest.php?act=add");
	  }
	}
}
elseif ($_REQUEST['act'] == 'delete')
{
	$smarty->assign('mm_show', 'DeleteSuggest');
	
	clear_caches();
	$smarty->display('suggest.tpl');
}

// 柱状图显示建议
elseif ($_REQUEST['act'] == 'pic')
{
	//$sdate = ' ';
	$start_date = isset($_POST['start_date']) ? $_POST['start_date'] : '';
	$end_date = isset($_POST['end_date']) ? $_POST['end_date'] : '';
	
	if (!empty($start_date) && !empty($end_date)){
		$sdate = " AND s_date >= '" . $start_date . "' AND s_date <= '" . $end_date . "'";
	} else {
		$sdate = '';
	}

	
	if (empty($_REQUEST['showall']))
	{
		$sql = "SELECT sum(s.s_actionability) AS acti, sum(s.s_innovate) AS innova, sum(s.s_effect) AS effect
			FROM " . $mmoa->table('suggest') . " AS s, " . $mmoa->table('employees') . " AS e " .
					" WHERE s.s_eid = e.id AND e.e_name = '" . $_SESSION['username'] . "' $sdate";
		$res = $db->query($sql);
		
		$row = $GLOBALS['db']->fetchRow($res);
		$score = $row['acti'] + $row['innova'] + $row['effect'];
		
		$username = "['".$_SESSION['username']."']";
		
		$smarty->assign('personpic', '1');
	}
	
	if ($_REQUEST['showall']){
		$name_score = suggest_name_score($sdate);  //print_r($name_score);		
		$username = "['" . $name_score[0]['name'] . "'"; //echo $username;
		$score = $name_score[0]['score']; //echo $score;
		
		for ($i = 1; $i < count($name_score); $i++)
		{
			$username .= ", '" . $name_score[$i]['name'] . "'";
			$score .= ',' . $name_score[$i]['score'];
		}
		
		$username .= "]";
		//$score = 10;

		$smarty->assign('allpic', '1');
	}
	
	// 权限判断
	$u = $_SESSION['username'];
	$sql = "SELECT e_depart, e_group FROM " . $mmoa->table('employees') . " WHERE e_name = '$u'";
	$uinfo = $db->getRow($sql);
	if ($uinfo){
		$d = $uinfo['e_depart'];
		$g = $uinfo['e_group'];
		
		$sqld = "SELECT d_name FROM " . $mmoa->table('departments') . " WHERE id = $d";
		$dname = $db->getOne($sqld);
		/*
		if ($dname == '管理部'){
			$priv = true;
		} elseif ($dname == '行政部' && $g == '部门经理') {
			$priv = true;
		} else {
			$priv = false;
		}*/
		$priv = true;
		
		$smarty->assign('priv', $priv);
	}
	
	
	$smarty->assign('score', $score);
	$smarty->assign('username', $username);
	$smarty->assign('mm_show', 'PicSuggest');
	
	clear_caches();
	$smarty->display('suggest.tpl');
}

//  审核建议
elseif ($_REQUEST['act'] == 'audit')
{
	//所有建议
	$sql = "SELECT * FROM " . $mmoa->table('suggest') . " WHERE 1 ORDER BY id DESC";
	$s = $db->getAll($sql);
	for ($i=0; $i<count($s); $i++){
		$eid = $s[$i]['s_eid'];
		$sqle = "SELECT e_name FROM " . $mmoa->table('employees') . " WHERE id=$eid";
		$ename = $db->getOne($sqle);
		
		if ($ename){
			$s[$i]['employee'] = $ename;
		}
	}
	$smarty->assign('suggest', $s);
	
	$smarty->assign('mm_show', 'auditSuggest');
	
	clear_caches();
	$smarty->display('suggest.tpl');
}
elseif ($_REQUEST['act'] == 'modaudit')
{
	// 审核
	$sid = $_GET['sid'];
	$sql = "SELECT * FROM " . $mmoa->table('suggest') . " WHERE id = $sid";
	$s = $db->getRow($sql);
	$smarty->assign('suggest', $s);
	
	$smarty->assign('mm_show', 'modaudit');
	
	clear_caches();
	$smarty->display('suggest.tpl');
}
elseif ($_REQUEST['act'] == 'modauditu')
{
	// 建议编号
	$sid = $_POST['sid'];
	//echo $sid;//exit();
	// 打分 实施
	$action = isset($_POST['actionability']) ? $_POST['actionability'] : 0;
	$innovate = isset($_POST['innovate']) ? $_POST['innovate'] : 0;
	$effect = isset($_POST['effect']) ? $_POST['effect'] : 0;
	$implementation = isset($_POST['implementation']) ? $_POST['implementation'] : 0;
	$dev = isset($_POST['devlop']) ? $_POST['devlop'] : '';
	
	if ($sid){
		$sql = "UPDATE "  . $mmoa->table('suggest') . " 
		       SET `s_actionability` = '$action',
		            `s_innovate` = '$innovate',
		            `s_effect` = '$effect',
		            `s_isimplementation` = '$implementation',
		            `s_isverify` = 1,		            
		            `s_devno` = '$dev'
		       WHERE id = $sid";
		if ($db->query($sql)){
			echo '<meta charset="utf-8">';
			echo '打分成功';		
		} else {
			echo '<meta charset="utf-8">';
			echo '打分失败';
		}
	}
}
elseif ($_REQUEST['act'] == 'del')
{
	// 删除建议
	$sid = $_GET['sid'];
	if ($sid){
		$sql = "DELETE FROM " . $mmoa->table('suggest') . " WHERE id = $sid";
		if ($db->query($sql)){
			echo '<meta charset="utf-8">';
			echo '删除成功';
		} else {
			echo '<meta charset="utf-8">';
			echo '删除失败';
		}
	}
}


//********************-- FUNCTIONS
function suggest_show($s)
{
	$sql = "SELECT * FROM " . $GLOBALS['mmoa']->table('suggest') .
	" where s_eid = '" . $s . "' ORDER BY id DESC";
	$res = $GLOBALS['db']->getAll($sql);
	
	for ($i = 0; $i < count($res); $i++)
	{
		$res[$i]['score'] = $res[$i]['s_actionability'] + $res[$i]['s_innovate'] + $res[$i]['s_effect'];
	}
	
	return $res;
}

function suggest_name_score($sdate)
{
	$sql0 = "SELECT id, e_name FROM " . $GLOBALS['mmoa']->table('employees');
	$employees = $GLOBALS['db']->getAll($sql0);
	
	$name_score = array();	
	for ($i = 0; $i < count($employees); $i++)
	{
		$name_score[$i]['name'] = $employees[$i]['e_name'];
		
		$sql = "SELECT sum(s_actionability) AS acti, sum(s_innovate) AS innova, sum(s_effect) AS effect FROM " . $GLOBALS['mmoa']->table('suggest') . " WHERE s_eid=" . $employees[$i]['id'] . " $sdate";		
		$row = $GLOBALS['db']->getRow($sql);
		
		$name_score[$i]['score'] = $row['acti'] + $row['innova'] + $row['effect'];
	}	
	
	return $name_score;	
}



